#include "ad_custom.h"
#include "ui_ad_custom.h"
#include "dlcw.h"
#include "value.h"
#include "ad_changeifm.h"
#include "value.h"
#include "admin_0.h"
#include "ad_sechname.h"
#include "hycz.h"
#include "xgmm_custom.h"
#include <string>
#include <QTimer>
#include <QDateTime>
#include <QHeaderView>
#include <QStandardItem>
#include <QMessageBox>

extern std::string _name;

ad_custom::ad_custom(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ad_custom)
{
    ui->setupUi(this);
    setWindowTitle("客户管理系统");
    themodel=new QStandardItemModel(js()+2,3,this);
    ui->tableView->setModel(themodel);
    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(updatename()));
    timer->start(1000); // 每次触发timeout信号时间间隔为1秒
}

ad_custom::~ad_custom()
{
    delete ui;
}

void ad_custom::on_action_4_triggered() // 返回主菜单
{
    this->close();
    Admin_0 *aa=new Admin_0(this);
    aa->show();
}


void ad_custom::on_action_2_triggered() //查看此会员账单
{
    QStringList headlist;
    headlist<<"时间"<<"金额"<<"收入来源或支出目的";
    themodel=new QStandardItemModel(js()+2,3,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    FILE* fp;              //文件指针
    int n = 0;
    int i = 12;
    float sum = 0, sum1 = 0, sum2 = 0;
    IAEP head = NULL;
    IAEP p, p2, pr = NULL;
    fp = fopen(_name.c_str(), "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       // printf("cannot open file\n");
        QMessageBox::warning(this,"err","文件打开失败");
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {                        //则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        n++;
        p = (IAEP)malloc(M);   //向内存申请一段空间
        fread(p, M, 1, fp);      //从fp所指向的文件读入1个大小为M的数据，存储到p所指向的空间中
        if (n == 1)
        {
            head = p;
            p2 = p;
            head->pre = NULL;

        }
        else                //创建双向链表
        {
            pr = p2;          //pr是p的前驱结点
            p->pre = p2;
            p2->next = p;
            p2 = p;
        }
    }
    int num=0;
    fclose(fp);             //关闭fp所指向的文件
    //printf("时     间          金   额                 收入来源或支出目的\n");
    QStandardItem *aItem;
    if (pr != NULL)
    {
        pr->next = NULL;
        p = pr;

        while (p != NULL)
        {
            //printf("%d.%d.%-12d%c%-24f%s\n", p->year, p->month, p->day, p->ch, p->iae, p->source_or_aim);
            aItem=new QStandardItem(QString::asprintf("%d.%d.%d",p->year, p->month, p->day));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,0,aItem);
            aItem=new QStandardItem(QString::asprintf("%c%f",p->ch, p->iae));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,1,aItem);
            aItem=new QStandardItem(QString::asprintf("%s",p->source_or_aim));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,2,aItem);
            if (p->ch == '+')
            {
                sum1 += p->iae;
            }
            else if (p->ch == '-')
            {
                sum2 -= p->iae;
            }
            sum = sum1 + sum2;
            i++;
            p = p->pre;
            num++;
        }
        aItem=new QStandardItem(QString::asprintf("剩余储值"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,0,aItem);
        aItem=new QStandardItem(QString::asprintf("总支出"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        aItem=new QStandardItem(QString::asprintf("总充值"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,2,aItem);
        num++;
        //printf("总   计： %f    总支出： %f    总收入： %f", sum, sum2, sum1);
        aItem=new QStandardItem(QString::asprintf("%f",sum));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,0,aItem);
        aItem=new QStandardItem(QString::asprintf("%f",sum2));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        aItem=new QStandardItem(QString::asprintf("%f",sum1));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,2,aItem);
    }
    else if (pr == NULL)
    {
        //printf("您还没记录任何信息哦~");
        aItem=new QStandardItem(QString::asprintf("该会员未记录任何信息"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
    }
}

void ad_custom::on_action_5_triggered()//会员充值
{
    _name=ui->cstmname->text().toStdString();
    hycz *a=new hycz(this);
    a->show();
}

int ad_custom::js()//查找该会员的帐单数
{
    FILE* fp;              //文件指针
    int n = 0;
    IAEP p;
    fp = fopen(_name.c_str(), "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       return 0;
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {
        p=(IAEP)malloc(M);//则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        fread(p, M, 1, fp);
        n++;
        free(p);
    }
    fclose(fp);
    return n;
}

int ad_custom::js_hy()//查找有多少个会员
{
    FILE* fp;              //文件指针
    int n = 0;
    Accp p;
    fp = fopen("account", "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       // printf("cannot open file\n");
        QMessageBox::warning(this,"err","文件打开失败");
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {
        p=(Accp)malloc(N);//则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        fread(p, N, 1, fp);
        n++;
        free(p);
    }
    fclose(fp);
    return n;
}

void ad_custom::on_action_3_triggered()//查看用户密码
{
    Accp pAccp = ff();
    int fl=1;
    if(_name=="admin123"){
        QMessageBox::warning(this,"err","无法查看管理员密码！");
        return;
    }
    while (strcmp(pAccp->name,_name.c_str())!=0) {
        pAccp = pAccp->next;
        if(pAccp==NULL){
            fl=0;
            break;
        }
    }
    if(fl==1){
        _password=pAccp->password;
        ui->custompswd->setText(QString::fromStdString(_password));
    }
}

void ad_custom::on_action_triggered()//查询会员
{
    ad_sechname *a=new ad_sechname(this);
    if(a->exec()==QDialog::Accepted){
        double suuum=Find_sum(_name.c_str());
        if(suuum>=300 && suuum<500)
        {
            ui->cstmrank->setText("青铜会员");
        }
        else if(suuum>=500&&suuum<1000)
        {
            ui->cstmrank->setText("白银会员");
        }
        else if(suuum>=1000)
        {
            ui->cstmrank->setText("黄金会员");
        }
        else{
            ui->cstmrank->setText("啊这，您的消费水平过低");
        }
    }
}

void ad_custom::on_action_6_triggered() //查看会员
{
    QStringList headlist;
    headlist<<"账户名"<<"剩余储值"<<"总支出"<<"总入账";
    themodel=new QStandardItemModel(js_hy(),4,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    //tableview自动适应大小
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    Accp pAccp = ff();
    QStandardItem *nItem;
    char custom_name[20];
    //printf("所有会员信息\n");
    int num = 0;
    while (pAccp->next!= NULL) {
        //printf("%d.账户名：%s \n", l, pAccp->name);
        if(pAccp==NULL) break;
        nItem=new QStandardItem(QString::asprintf("%s",pAccp->name));
        nItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,0,nItem);
        strcpy(custom_name, pAccp->name);
        FILE* fp;              //文件指针
        int n = 0;
        float sum = 0, sum1 = 0, sum2 = 0;
        IAEP head = NULL;
        IAEP lp, p2, pr = NULL;
        fp = fopen(custom_name, "ab+");//打开文件，方式为只读
        if (fp == NULL)            //如果打开为空
        {
            QMessageBox::warning(this,"err","打开文件失败！");
            //printf("cannot open file\n");
            break;

        }
        while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
        {                        //则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
            n++;
            lp = (IAEP)malloc(M);   //向内存申请一段空间
            fread(lp, M, 1, fp);      //从fp所指向的文件读入1个大小为M的数据，存储到p所指向的空间中
            if (n == 1)
            {
                head = lp;
                p2 = lp;
                head->pre = NULL;
            }
            else                //创建双向链表
            {
                pr = p2;          //pr是p的前驱结点
                lp->pre = p2;
                p2->next = lp;
                p2 = lp;
            }
        }
        fclose(fp);             //关闭fp所指向的文件
        if (pr != NULL)
        {
            pr->next = NULL;
            lp = pr;
            while (lp != NULL)
            {
                if (lp->ch == '+')
                {
                    sum1 += lp->iae;
                }
                else if (lp->ch == '-')
                {
                    sum2 -= lp->iae;
                }
                sum = sum1 + sum2;
                lp = lp->pre;
            }
            //printf("总   计： %f    总支出： %f    总收入： %f", sum, sum2, sum1);
            nItem=new QStandardItem(QString::asprintf("%f",sum));
            nItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,1,nItem);
            nItem=new QStandardItem(QString::asprintf("%f",sum2));
            nItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,2,nItem);
            nItem=new QStandardItem(QString::asprintf("%f",sum1));
            nItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,3,nItem);
        }
        else if (pr == NULL)
        {
           // printf("该会员还没记录任何信息");
            nItem=new QStandardItem(QString::asprintf("该会员未记录任何信息"));
            nItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,1,nItem);
        }
        pAccp = pAccp->next;
        num++;
    }
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}

void ad_custom::updatename()//实时更新用户名
{
    ui->cstmname->setText(QString::fromStdString(_name));
}

double Find_sum(const char ad_name[20])//计算会员总储值
{
    FILE* fp;              //文件指针
    int n = 0;
    float sum2 = 0;
    IAEP head = NULL;
    IAEP xp, p2, pr = NULL;
    fp = fopen(ad_name, "ab+");//打开文件，方式为只读
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {                        //则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        n++;
        xp = (IAEP)malloc(M);   //向内存申请一段空间
        fread(xp, M, 1, fp);      //从fp所指向的文件读入1个大小为M的数据，存储到p所指向的空间中
        if (n == 1)
        {
            head = xp;
            p2 = xp;
            head->pre = NULL;
        }
        else                //创建双向链表
        {
            pr = p2;          //pr是p的前驱结点
            xp->pre = p2;
            p2->next = xp;
            p2 = xp;
        }
    }
    fclose(fp);             //关闭fp所指向的文件
    if (pr != NULL)
    {
        pr->next = NULL;
        xp = pr;
        while (xp != NULL)
        {
            if (xp->ch == '+')
            {
                sum2 += xp->iae;
            }
            xp = xp->pre;
        }
    }
    return sum2;
}

void ad_custom::on_action_7_triggered()//修改该会员密码
{
    xgmm_custom *xg=new xgmm_custom(this);
    xg->show();
}
